package pl.enigmatic.time;

import pl.enigmatic.util.Time;


public class Timer extends java.util.Timer {

	/** in seconds */
	public static final double DEFAULT_PERIOD = 1.0 / 60;

	private final String name;

	public Timer(final String name, final boolean isDaemon) {
		super(name, isDaemon);
		this.name = name;
	}

	public Timer(final boolean isDaemon) {
		this("", isDaemon);
	}

	public Timer(final String name) {
		this(name, true);
	}

	public Timer() {
		this("");
	}

	public String getName() {
		return name;
	}

	/**
	 * @param task
	 * @param delay the delay in seconds
	 * @param period the period in seconds
	 * @return <code>true</code>, if the <code>task</code> was succesfully scheduled on <code>this</code> timer, <code>false</code> - if the schedule
	 *         operation could not complete, because the <code>task</code> was already scheduled
	 */
	public boolean schedule(final TimerTask task, final double delay, final double period) {
		if (task.setTimerAndSchedule(this)) {
			super.schedule(task, Time.secondsToMilli(delay), Time.secondsToMilli(period));
			return true;
		}
		return false;
	}

	public boolean schedule(final TimerTask task) {
		return schedule(task, 0.0, DEFAULT_PERIOD);
	}

}
